
package net.qiqbframework.serialization;

/**
 * 转化
 */
public interface Converter {


    boolean canConvert(Class<?> sourceType, Class<?> targetType);


    default <T> T convert(Object original, Class<T> targetType) {
        return convert(original, original.getClass(), targetType);
    }

    <T> T convert(Object original, Class<?> sourceType, Class<T> targetType);

    @SuppressWarnings("unchecked")
    default <T> SerializedObject<T> convert(SerializedObject<?> original, Class<T> targetType) {
        if (original.getContentType().equals(targetType)) {
            return (SerializedObject<T>) original;
        }
        return new SimpleSerializedObject<>(convert(original.getData(), original.getContentType(), targetType),
                                            targetType, original.getType());
    }

}
